package com.zhihuishu.micro.course.service.impl;

import com.zhihuishu.micro.course.dao.ChapterTreenityMapper;
import com.zhihuishu.micro.course.dto.ChapterDto;
import com.zhihuishu.micro.course.model.Chapter;
import com.zhihuishu.micro.course.service.ChapterTreenityDBService;
import com.zhihuishu.micro.course.service.CourseService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Service("chapterAllServiceDB")
public class ChapterAllServiceDBImpl extends BaseTreenityBatchServiceDBImpl<ChapterDto,Chapter> implements ChapterTreenityDBService {

    @Resource
    private ChapterTreenityMapper chapterTreenityMapper;

    @Resource
    private CourseService courseService;

    @Override
    public ChapterDto detail(Integer chapterId) {
        Chapter chapter = chapterTreenityMapper.get(chapterId);
        ChapterDto chapterDto = toDto(chapter);
        return chapterDto;
    }

    @Override
    public void update(ChapterDto chapterDto, long updateUserId) {
        Chapter model = toModel(chapterDto) ;
        chapterTreenityMapper.update(model);
    }

    @Override
    public void delete(Integer chapterId, long deleteUserId) {
        chapterTreenityMapper.remove(chapterId);
    }

    @Override
    public void sort(List<ChapterDto> dbDtoList, long updateUserId) {
        List<Chapter> modelList = new LinkedList<Chapter>();
        for (ChapterDto chapterDto : dbDtoList) {
            modelList.add(toModel(chapterDto));
        }
        chapterTreenityMapper.sort(modelList);
    }

    @Override
    public Integer create(ChapterDto chapter, long creator) {
        Chapter model = toModel(chapter);
        model.setPersonalCourseId(courseService.getLinkCourseId(chapter.getCourseId()));

        chapterTreenityMapper.insertSelective(model);

        Integer chapterId = model != null ? model.getId() : null;
        chapter.setId(chapterId);

        return chapterId;
    }

    public Chapter toModel(ChapterDto dto) {
        if (dto == null) return null;
        Chapter model = new Chapter();
        model.setDescription(dto.getDescription());
        model.setId(dto.getId());
        model.setLimitDay(dto.getLimitDay());
        model.setName(dto.getName());
        model.setOrderNumber(dto.getRank());
        model.setStudyHour(dto.getStudyHour());
        model.setPersonalCourseId(dto.getPersonalCourseId());
        model.setIsDeleted(dto.getIsDeleted());
        model.setCourseId(dto.getCourseId());
        model.setIsPass(dto.getIsPass());
        //model.setSpeaker(dto.getSpeaker());
        return model;
    }


    /**
     * Model转换DTO
     *
     * @param model
     *         章Model对象
     *
     * @return
     */
    public ChapterDto toDto(Chapter model) {
        if (model == null) return null;
        ChapterDto dto = new ChapterDto();
        dto.setDescription(model.getDescription());
        dto.setId(model.getId());
        dto.setIsPass(model.getIsPass());
        dto.setLimitDay(model.getLimitDay());
        dto.setName(model.getName());
        dto.setRank(model.getOrderNumber());
        dto.setStudyHour(model.getStudyHour());

        // 课程ID需要使用TBL_COURSE主键
        if (model.getPersonalCourseId() != null) {
            dto.setCourseId(courseService.getByLinkId(model.getPersonalCourseId()));
        }

        return dto;
    }

    @Override
    public List<Integer> listDtoIds(long courseId) {
        Long tmpLinkCourseId = null;
        try {
            tmpLinkCourseId = courseService.getLinkCourseId(courseId);

        } catch (Exception e) {
            e.printStackTrace();
        }
        List<Integer> chapterIds;
        if (tmpLinkCourseId != null) {
            chapterIds = chapterTreenityMapper.list(tmpLinkCourseId);
        } else {
            chapterIds = chapterTreenityMapper.listByCourseId(courseId);
        }
        return chapterIds;
    }

    @Override
    public List<ChapterDto> list(List<Integer> listChapterIds) {
        final List<ChapterDto> chapters = new ArrayList<ChapterDto>();
        if (!CollectionUtils.isEmpty(listChapterIds)) {
            List<Chapter> list = chapterTreenityMapper.getList(listChapterIds);
            for (Chapter chapter : list) {
                chapters.add(toDto(chapter));
            }
        }
        return chapters;
    }

    @Override
    public void deleteMulti(List<Integer> dtoIds, long deleteUserId) {

    }
}
